﻿#include "MailInfo.h"
#include "../Settings.h"

using namespace Server::MirDatabase;
using namespace Server::MirObjects;

namespace Server::MirEnvir
{

    Server::MirEnvir::Envir *MailInfo::getEnvir()
    {
        return Envir::getMain();
    }

    bool MailInfo::getSent() const
    {
        return DateSent > DateTime::MinValue;
    }

    bool MailInfo::getOpened() const
    {
        return DateOpened > DateTime::MinValue;
    }

    bool MailInfo::getParcel() const
    {
        return Gold > 0 || Items.size() > 0;
    }

    MailInfo::MailInfo(int recipientIndex, bool canReply)
    {
        setEnvir(getEnvir() + 1);
        MailID = getEnvir()->NextMailID;
        RecipientIndex = recipientIndex;

        CanReply = canReply;
    }

    MailInfo::MailInfo(BinaryReader *reader, int version, int customversion)
    {
        MailID = reader->ReadUInt64();
        Sender = reader->ReadString();
        RecipientIndex = reader->ReadInt32();
        Message = reader->ReadString();
        Gold = reader->ReadUInt32();

        int count = reader->ReadInt32();

        for (int i = 0; i < count; i++)
        {
            UserItem *item = new UserItem(reader, version, customversion);
            if (getEnvir()->BindItem(item))
            {
                Items.push_back(item);
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete item' statement was not added since item was passed to a method or constructor. Handle memory management manually.
        }

        DateSent = DateTime::FromBinary(reader->ReadInt64());
        DateOpened = DateTime::FromBinary(reader->ReadInt64());

        Locked = reader->ReadBoolean();
        Collected = reader->ReadBoolean();
        CanReply = reader->ReadBoolean();
    }

    void MailInfo::Save(BinaryWriter *writer)
    {
        writer->Write(MailID);
        writer->Write(Sender);
        writer->Write(RecipientIndex);
        writer->Write(Message);
        writer->Write(Gold);

        writer->Write(Items.size());
        for (int i = 0; i < Items.size(); i++)
        {
            Items[i]->Save(writer);
        }

        writer->Write(DateSent.ToBinary());
        writer->Write(DateOpened.ToBinary());

        writer->Write(Locked);
        writer->Write(Collected);
        writer->Write(CanReply);
    }

    void MailInfo::Send()
    {
        if (getSent())
        {
            return;
        }

        Collected = true;

        if (getParcel())
        {
            if (Items.size() > 0 && Gold > 0)
            {
                if (!Settings::MailAutoSendGold || !Settings::MailAutoSendItems)
                {
                    Collected = false;
                }
            }
            if (Items.size() > 0)
            {
                if (!Settings::MailAutoSendItems)
                {
                    Collected = false;
                }
            }
            else
            {
                if (!Settings::MailAutoSendGold)
                {
                    Collected = false;
                }
            }
        }

        if (RecipientInfo == nullptr)
        {
            RecipientInfo = getEnvir()->GetCharacterInfo(RecipientIndex);
        }

        RecipientInfo->Mail.push_back(this); //add to players inbox

        if (RecipientInfo->Player != nullptr)
        {
            RecipientInfo->Player->NewMail = true; //notify player of new mail  --check in player process
        }

        DateSent = DateTime::Now;
    }

    ClientMail *MailInfo::CreateClientMail()
    {
        ClientMail *tempVar = new ClientMail();
        tempVar->MailID = MailID;
        tempVar->SenderName = Sender;
        tempVar->Message = Message;
        tempVar->Locked = Locked;
        tempVar->CanReply = CanReply;
        tempVar->Gold = Gold;
        tempVar->Items = Items;
        tempVar->Opened = getOpened();
        tempVar->Collected = Collected;
        tempVar->DateSent = DateSent;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was used in a 'return' or 'throw' statement.
        return tempVar;
    }
}
